package com.itheima.service.omsService.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.oms.OmsOrder;
import com.itheima.dto.OmsOrderDto;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.omsMapper.OrderMapper;
import com.itheima.service.omsService.OrderService;
import com.itheima.util.TimeUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author DogeGuo
 */
@DubboService
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询订单
     *
     * @param omsOrderDto 参数
     * @param pageNum     页数
     * @param pageSize    每页条数
     * @return
     */
    @Override
    public  PageBean<OmsOrder> findByParams(OmsOrderDto omsOrderDto, Integer pageNum, Integer pageSize) {

        IPage<OmsOrder> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();

        //时间格式处理  String -> Data
        if (omsOrderDto != null) {
            String time = omsOrderDto.getCreateTime();
            if (time != null) {
                Date createTime = TimeUtil.getTimeForStr(time);
                //订单时间 编号 订单类型 订单来源 订单状态
                wrapper.eq(ObjectUtil.isNotEmpty(createTime), OmsOrder::getCreateTime, createTime);
            }

            wrapper.eq(StrUtil.isNotEmpty(omsOrderDto.getOrderSn()), OmsOrder::getOrderSn, omsOrderDto.getOrderSn());
            wrapper.eq(ObjectUtil.isNotEmpty(omsOrderDto.getOrderType()), OmsOrder::getOrderType, omsOrderDto.getOrderType());
            wrapper.eq(ObjectUtil.isNotEmpty(omsOrderDto.getSourceType()), OmsOrder::getSourceType, omsOrderDto.getSourceType());
            wrapper.eq(ObjectUtil.isNotEmpty(omsOrderDto.getStatus()), OmsOrder::getStatus, omsOrderDto.getStatus());

            //收货人姓名/号码
            wrapper.like(StrUtil.isNotEmpty(omsOrderDto.getReceiverKeyword()),
                            OmsOrder::getReceiverName, omsOrderDto.getReceiverKeyword())
                    .or().like(StrUtil.isNotEmpty(omsOrderDto.getReceiverKeyword()),
                            OmsOrder::getReceiverPhone, omsOrderDto.getReceiverKeyword());
        }

        //创建条件对象，执行分页操作
        orderMapper.selectPage(page, wrapper);

        PageBean<OmsOrder> pageBean = new PageBean<>(page);

        return pageBean;
    }

    /**
     * 根据id获取商品数据
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrder getById(Integer id) {
        return orderMapper.selectById(id);
    }

    /**
     * 批量删除订单
     *
     * @param ids
     */
    @Override
    public void updateDelete(Integer[] ids) {
        for (Integer id : ids) {
            orderMapper.deleteById(id);
        }
    }

    /**
     * 批量关闭订单
     *
     * @param ids
     * @param note
     */
    @Override
    public void updateClose(Integer[] ids, String note) {
        for (Integer id : ids) {
            orderMapper.updateClose(id, note);
        }
    }

    /**
     * 批量发货
     *
     * @param maps
     */
    @Override
    public void updateDelivery(LinkedHashMap[] maps) {
        if (maps == null) {
            throw new BusinessException("没有参数");
        }

        for (LinkedHashMap map : maps) {
            String deliveryCompany = StrUtil.toString(map.get("deliveryCompany"));
            String deliverySn = StrUtil.toString(map.get("deliverySn"));
            Integer orderId = Convert.toInt(map.get("orderId"));

            if (orderId == null) {
                throw new BusinessException("没有订单Id");
            }

            //封装订单
            OmsOrder order = new OmsOrder();
            order.setId(Convert.toLong(orderId));
            //物流公司
            order.setDeliveryCompany(deliveryCompany);
            //物流单号
            order.setDeliverySn(deliverySn);
            //订单状态:3=>已发货
            order.setStatus(3);

            orderMapper.updateById(order);
        }
    }

    /**
     * 修改收货人信息
     *
     * @param map
     */
    @Override
    public void updateReceiverInfo(Map map) {
        if (map == null) {
            throw new BusinessException("没有数据");
        }
        OmsOrder order = new OmsOrder();
        order.setId(Convert.toLong(map.get("orderId")));//订单id
        order.setReceiverCity(StrUtil.toString(map.get("receiverCity")));//城市
        order.setReceiverDetailAddress(StrUtil.toString(map.get("receiverDetailAddress")));//详细地址
        order.setReceiverName(StrUtil.toString(map.get("receiverName")));//收货人姓名
        order.setReceiverPhone(StrUtil.toString(map.get("receiverPhone")));//收货人电话
        order.setReceiverPostCode(StrUtil.toString(map.get("receiverPostCode")));//收货人邮编
        order.setReceiverProvince(StrUtil.toString(map.get("receiverProvince")));//省份/直辖市
        order.setReceiverRegion(StrUtil.toString(map.get("receiverRegion")));//区
        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        order.setStatus(Convert.toInt(map.get("status")));

        orderMapper.updateById(order);
    }

    /**
     * 备注订单
     *
     * @param id
     * @param note
     */
    @Override
    public void updateNote(Integer id, String note, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(Convert.toLong(id));
        order.setNote(note);
        order.setStatus(status);
        orderMapper.updateById(order);
    }

    /**
     * 修改订单费用信息
     *
     * @param map
     */
    @Override
    public void updateMoneyInfo(Map map) {
        OmsOrder order = new OmsOrder();

        //订单Id
        order.setId(Convert.toLong(map.get("orderId")));
        //管理员后台调整订单所使用的折扣金额
        order.setDiscountAmount(Convert.toBigDecimal(map.get("discountAmount")));
        //运费金额
        order.setFreightAmount(Convert.toBigDecimal(map.get("freightAmount")));
        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        order.setStatus(Convert.toInt(map.get("status")));

        orderMapper.updateById(order);
    }
}
